package com.bainuo.doctor.common.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bainuo.doctor.common.R;
import com.bainuo.doctor.common.widget.wheelview.OnWheelChangedListener;
import com.bainuo.doctor.common.widget.wheelview.OnWheelScrollListener;
import com.bainuo.doctor.common.widget.wheelview.WheelView;
import com.bainuo.doctor.common.widget.wheelview.adapter.AbstractWheelTextAdapter;
import com.blankj.utilcode.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by faraway on 28/2/17.
 */
public class BNWheelViewDlg {

    private CalendarTextAdapter mAdpter;
    private int maxTextSize = 16;
    private int minTextSize = 12;

    private String mCurrceText;//选择的文字
    private int mCurrceIndex;//当前的下标
    private Dialog mDialog;
    public interface Listener {

        void onSelect(String string, int index);

        void onEnd();
    }

    public void show(Context context, final List<String> list, final Listener listener) {

        mDialog = new Dialog(context, R.style.DialogStyle);
        mDialog.setCancelable(false);
        //在选择框外面则销毁弹出框
        mDialog.setCanceledOnTouchOutside(true);
        View root = LayoutInflater.from(context).inflate(
                R.layout.dlg_wheel, null);
        mDialog.setContentView(root);
        TextView tvOk = (TextView) root.findViewById(R.id.dlg_wheel_tv_ok);

        WheelView wheelView = (WheelView) root.findViewById(R.id.dlg_wheel);

        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (listener != null)
                    listener.onEnd();
            }
        });
//        this.list.addAll(list);

        // 最后一个参数  设置当前的字体变大  索引的位置
        mAdpter = new CalendarTextAdapter(context, list, 0);
        wheelView.setVisibleItems(5); //显示多少行
        wheelView.setViewAdapter(mAdpter);

        //赋值默认值
        if(list != null && list.size() > 0){
            mCurrceText = list.get(0);
            mCurrceIndex = 0;
        }
        wheelView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                LogUtils.e("123", "OnWheelChangedListener---->" + oldValue + " " + newValue);
                String currentText = (String) mAdpter.getItemText(wheel.getCurrentItem());
//                setTextviewSize(currentText, mAdpter);
                mCurrceText = currentText;
                mCurrceIndex = wheel.getCurrentItem();
            }
        });

//        wheelView.addScrollingListener(new OnWheelScrollListener() {
//
//            @Override
//            public void onScrollingStarted(WheelView wheel) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onScrollingFinished(WheelView wheel) {
//                // TODO Auto-generated method stub
//                String currentText = (String) mAdpter.getItemText(wheel.getCurrentItem());
//                setTextviewSize(currentText, mAdpter);
//            }
//        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!TextUtils.isEmpty(mCurrceText) && listener != null)
                    listener.onSelect(mCurrceText, mCurrceIndex);
                mDialog.dismiss();
            }
        });
        root.findViewById(R.id.dlg_wheel_tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        Window mWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth(context);
        mWindow.setGravity(Gravity.BOTTOM);
        //添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        if(mDialog != null && !mDialog.isShowing()){
            mDialog.show();
        }

    }


    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(maxTextSize);
            } else {
                textvew.setTextSize(minTextSize);
            }
        }
    }

    private class CalendarTextAdapter extends AbstractWheelTextAdapter {
        List<String> list;

        protected CalendarTextAdapter(Context context, List<String> list, int currentItem) {
            super(context, R.layout.wheekl_item, NO_RESOURCE, currentItem, maxTextSize, minTextSize);
            this.list = list;
            setItemTextResource(R.id.reply_dialog_item_tv_content);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            view.findViewById(R.id.replay_dialog_line).setVisibility(View.VISIBLE);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index) + "";
        }

    }


    /**
     * 获取屏幕分辨率宽
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取屏幕分辨率高
     */
    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }
}
